#/usr/bin/python
from util import Util
import re

class SQLParser():
    def parser(self,sentences):
        util=Util()
        for sentence in sentences.split("|"):
            ids=re.split("[,]",sentence)
            if ids.__len__()==1:
                ids=re.split("[ ]",ids[0])
                yield (util.javaid_to_sqlid(ids[0]),util.javatype_to_sqltype(ids[1]))
            elif ids.__len__()==2:
                columnstr=re.split("[;]",ids[0])[0]
                yield (util.javaid_to_sqlid(columnstr),ids[1])
    def __clean__(self,ids):
        for id in ids:
            if re.findall('\w+',id).__len__()>=0:
                ids.remove(id)
        return ids

    def run_parser(self,tablename,sentences):
        sql_table=SQLTable(tablename)
        for ids in self.parser(sentences):
            sql_table.add_list(ids[0],ids[1])
        return sql_table.gen_create_Table()

class SQLTable:
    def __init__(self,tablename):
        #table:java id,java type
        self.table=[]
        self.tablename=tablename
        self.add_list("id","int auto_increment primary key")
    def add_list(self,sql_id,sql_type):
        self.table.append((sql_id,sql_type))
    def gen_create_Table(self):
        create_sql=None
        for column in self.table:
            if create_sql==None:
                create_sql="create table "+self.tablename +"("+column[0]+" "+column[1]
            else:
                create_sql=create_sql+","+column[0]+" "+column[1]
        create_sql=create_sql+")"
        return create_sql
            

if __name__=="__main__":
    parser_file="PictureUrl String varchar(20)|SetTime Timestamp,timestamp default now()|Sid Long"
    #print(sql_table.gen_create_Table())
    sqlparser=SQLParser()
    print(sqlparser.run_parser("itemid",parser_file))


